<?php

namespace App\Admin\Controllers;

use App\Models\Feedback;

use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class FeedbackController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Feedback::class, function (Grid $grid) {
            $grid->model()->orderBy('id','desc');

            $grid->id('ID')->sortable();
            $grid->user_id('用户');
            $grid->column('', "用户手机")->display(function (){
                $model = User::find($this->user_id);
                return $model? $model->mobile:'';
            });
            $grid->column('status', "处理状态")->display(function (){
                return $this->status > 0 ? '已处理' : '未处理';
            });
            $grid->disableCreation();
            $grid->filter(function(Grid\Filter $filter){
                // 禁用id查询框
                $filter->disableIdFilter();
                $filter->where(function ($query) {
                    $user = User::where('mobile',$this->input)->first();
                    $query->where('user_id',$user->id);

                }, '手机号');
            });
            $grid->type('状态');
            $grid->content('内容');
            $grid->created_at('创建时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Feedback::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->select("status","处理状态")->options(function($v){
               $map[Feedback::STATUS_PROCESS] = '已处理';
               $map[Feedback::STATUS_WAIT] = '未处理';
               return $map;
            });
        });
    }
}
